package com.woniuxy.woniuhis.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.woniuhis.entity.Examination;
import com.woniuxy.woniuhis.entity.ExaminationItem;
import com.woniuxy.woniuhis.entity.ExaminationResult;
import com.woniuxy.woniuhis.exception.ExaminationResultServiceException;
import com.woniuxy.woniuhis.mapper.ExaminationItemMapper;
import com.woniuxy.woniuhis.mapper.ExaminationMapper;
import com.woniuxy.woniuhis.mapper.ExaminationResultMapper;
import com.woniuxy.woniuhis.service.ExaminationResultService;

@Service
public class ExaminationResultServiceImpl implements ExaminationResultService {

	@Autowired
	ExaminationResultMapper examinationResultMapper;

	@Autowired
	ExaminationItemMapper examinationItemMapper;

	@Autowired
	ExaminationMapper examinationMapper;

	/**
	 * 	添加检查结果,同时修改检查状态
	 */
	@Override
	@Transactional
	public void addExaminationResult(ExaminationResult examinationResult) {
		try {
			// 添加检查结果记录
			// 需要判断是门诊病人还是住院病人
			// 门诊病人需要先缴费
			// TODO 这里需要门诊部门的编号
			// 先查检查条目表,获取到检查条目
			ExaminationItem examinationItem = examinationItemMapper.selectExaminationItemById(examinationResult.getExamItemId());
			if(examinationItem == null) {
				throw new ExaminationResultServiceException("检查条目的单号不存在");
			}
			//根据检查条目查检查单
			Examination examination = examinationMapper.selectExaminationById(examinationItem.getExaminationId());
			if(examination == null) {
				throw new ExaminationResultServiceException("检查单不存在");
			}
			if(examination.getSourceId().startsWith("in")) {
				examinationResultMapper.insertExaminationResult(examinationResult);
				examinationItemMapper.updateExaminationItemById(true, examinationItem.getExamItemId());
			}else if(examination.getSourceId().startsWith("out") && examination.getPayStatus()){
				examinationResultMapper.insertExaminationResult(examinationResult);
				examinationItemMapper.updateExaminationItemById(true, examinationItem.getExamItemId());
			}else {
				throw new ExaminationResultServiceException("门诊病人需要先缴费再检查");
			}
			
		} catch (Exception e) {
			e.printStackTrace();
			throw new ExaminationResultServiceException(e.getMessage());
		}
		

	}

	/**
	 * 根据检查结果id删除一条检查结果
	 */
	@Override
	public void delExaminationResultById(String id) {
		try {
			examinationResultMapper.deleteExaminationResultById(id);
		} catch (Exception e) {
			e.printStackTrace();
			throw new ExaminationResultServiceException("系统维护中");
		}
	}

	/**
	 * 	根据检查结果id更新检查结果
	 */
	@Override
	public void modifyExaminationResultById(ExaminationResult examinationResult) {
		try {
			examinationResultMapper.updateExaminationResultById(examinationResult);
		} catch (Exception e) {
			e.printStackTrace();
			throw new ExaminationResultServiceException("系统维护中");
		}
	}

	/**
	 * 
	 * 	根据检查结果id查询检查结果
	 */
	@Override
	public ExaminationResult findExaminationResultByid(String id) {
		try {
			return examinationResultMapper.selectExaminationResultByid(id);
		} catch (Exception e) {
			e.printStackTrace();
			throw new ExaminationResultServiceException("系统维护中");
		}
	}

	/**
	 *	 根据检查条目id查询检查结果
	 */
	@Override
	public ExaminationResult findExaminationResultByExamItemId(String examItemId) {
		try {
			return examinationResultMapper.selectExaminationResultByExamItemId(examItemId);
		} catch (Exception e) {
			e.printStackTrace();
			throw new ExaminationResultServiceException("系统维护中");
		}
	}

	/**
	 * 	查询所有检查结果,分页查询
	 */
	@Override
	public PageInfo<ExaminationResult> findAllExaminationResults(int pageIndex, int pageSize) {
		try {
			PageHelper.startPage(pageIndex, pageSize);
			List<ExaminationResult> examinationResults = examinationResultMapper.selectAllExaminationResults();
			PageInfo<ExaminationResult> pageInfo = new PageInfo<ExaminationResult>(examinationResults);
			return pageInfo;
		} catch (Exception e) {
			e.printStackTrace();
			throw new ExaminationResultServiceException("系统维护中");
		}

	}
	/**
	 * 	 根据组合条件查询检查结果
	 */
	@Override
	public PageInfo<ExaminationResult> findExaminationResultByCondition(ExaminationResult examinationResult,
			int pageIndex, int pageSize) {
		try {
			PageHelper.startPage(pageIndex, pageSize);
			List<ExaminationResult> examinationResults = examinationResultMapper
					.selectExaminationResultByCondition(examinationResult);
			PageInfo<ExaminationResult> pageInfo = new PageInfo<ExaminationResult>(examinationResults);
			return pageInfo;
		} catch (Exception e) {
			e.printStackTrace();
			throw new ExaminationResultServiceException("系统维护中");
		}

	}
	/**
	 * 	 根据组合条件查询检查结果
	 */
	@Override
	public List<ExaminationResult> findExaminationResultByCondition(ExaminationResult examinationResult) {
		try {
			return examinationResultMapper.selectExaminationResultByCondition(examinationResult);
		} catch (Exception e) {
			e.printStackTrace();
			throw new ExaminationResultServiceException("系统维护中");
		}
	}

}
